package com.test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CountDownLatchDemo1 {
    private static int start = 1;
    private static int mid =5;
    private static int end=10;
    private static volatile int num1,mum2;
    public static int addSum(int a,int b){
        int sum =0;
        for (int i = a; i <= b; i++) {
            sum+=i;
        }
        return sum;
    }

    public static  int sum(int a,int c) {
        return a + c;
    }
    public static  int sub(int a,int c) {
        return a - c;
    }
    public static  long mul(int a,int c) {
        return a * c;
    }

    public static void main(String[] args) {
        CountDownLatch  downLatch = new CountDownLatch(2);
        ExecutorService service = Executors.newFixedThreadPool(4);
        service.execute(()->{
            try {
                downLatch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            int sum = sum(num1, mum2);
            //int sub = sub(mum2, num1);
            System.out.println("sum = " + sum);
            //System.out.println("sub = " + sub);
        });
       service.execute(()->{
           try {
               downLatch.await();
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
           int sub = sub(mum2, num1);
            System.out.println("sub = " + sub);
        });
        service.execute(()->{
            try {
                downLatch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            long mul = mul(mum2, num1);
            System.out.println("mul = " + mul);
        });
        service.execute(()->{
            num1 = addSum(start, mid);
            System.out.println("num1是"+start +"-"+mid + "的和= " + num1);
            downLatch.countDown();
        });
        service.execute(()->{
            mum2 = addSum(mid+1, end);
            System.out.println("num2是"+(mid+1) +"-"+end + "的和= " + mum2);
            downLatch.countDown();
        });
        service.shutdown();

    }

}
